using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using SypGui.CreateForms;
using SypEngine;
using SypGui.ConnectionUC;
using SypConst.Garbage;

namespace SypGui.OptionForms
{
    public partial class siteControlPanel : SypUserControl
    {
        private SypPrefForm.SypPrefStatusText StatusBarText = null;
        private SypUCConnection myUCConnexion = null;
        private SypSite mySite = null;

        protected override void InitializeRessources()
        {
            SiteControlPanelLabelUrl.Text = GetRessources("OptionSiteHostname");
        }

        public override void ProcessConfig()
        {
            OutputMethod("Edit-site" + SypGarbage.COMMAND_SEPARATOR + SiteId + SypGarbage.COMMAND_SEPARATOR  + "\"" + SiteControlPanelTextBoxUrl.Text + "\"", Output.OUT);
            OutputMethod("Edit-site" + SypGarbage.COMMAND_SEPARATOR + SiteId + SypGarbage.COMMAND_SEPARATOR  + "\"" + SiteControlPanelTextBoxUrl.Text + "\"", Output.COMMAND);

            myUCConnexion.ProcessConfig();
        }

        public void RefreshConfig()
        {
            // Retrieve Site settings
            SiteControlPanelGroupBox.Text = mySite.SiteTitle;
            SiteControlPanelTextBoxUrl.Text = mySite.SiteUrl;
        }

        private void InitPanel()
        {
            // Initialize User Control size and positions
            Location = new System.Drawing.Point(119, 10);
            Name = mySite.SiteId.ToString(); ;
            RightToLeft = System.Windows.Forms.RightToLeft.Yes;
            SiteId = mySite.SiteId;
            Size = new System.Drawing.Size(354, 236);
            TabIndex = 3;

            // Initialize UC Connexion
             switch (mySite.SiteConnectionMode)
            {
                case SypConst.Garbage.ConnectionMode.FTP_MODE:
                    {
                        myUCConnexion = new FtpStep2(OutputMethod);
                        ((FtpStep2)myUCConnexion).FtpHostnameText = mySite.SypTfConf.Hostname;
                        ((FtpStep2)myUCConnexion).FtpUserText = mySite.SypTfConf.Login;
                        ((FtpStep2)myUCConnexion).FtpPasswordText = mySite.SypTfConf.Password;
                        ((FtpStep2)myUCConnexion).FtpPortText = mySite.SypTfConf.Port;
                        ((FtpStep2)myUCConnexion).FtpRemotePathText = mySite.SypTfConf.RemoteDir;

                        if (mySite.SypTfConf.Login.Equals("") && mySite.SypTfConf.Password.Equals(""))
                            ((FtpStep2)myUCConnexion).FtpAnonymousCheck = true;
                        else
                            ((FtpStep2)myUCConnexion).FtpAnonymousCheck = false;

                        break;
                    }
                case SypConst.Garbage.ConnectionMode.DIRECT_MODE:
                    {
                        myUCConnexion = new DirectStep2(OutputMethod);
                        ((DirectStep2)myUCConnexion).LocalRemotePath = mySite.SypTfConf.RemoteDir;
                        
                        break;
                    }
                case SypConst.Garbage.ConnectionMode.SCP_MODE:
                    {
                        myUCConnexion = new ScpStep2(OutputMethod);
                        ((ScpStep2)myUCConnexion).ScpHostnameText = mySite.SypTfConf.Hostname;
                        ((ScpStep2)myUCConnexion).ScpUserText = mySite.SypTfConf.Login;
                        ((ScpStep2)myUCConnexion).ScpPasswordText = mySite.SypTfConf.Password;
                        ((ScpStep2)myUCConnexion).ScpRemotePathText = mySite.SypTfConf.RemoteDir;
                        break;
                    }
                default:
                    {
                        OutputMethod("[Option Panel] - " + SiteId + " : No settings UC available for this connectionMode", Output.OUT);
                        break;
                    }
            }

            if (myUCConnexion != null)
            {
                myUCConnexion.SiteId = mySite.SiteId;
                myUCConnexion.Location = new System.Drawing.Point(0, 107);
                myUCConnexion.Name = "FtpUc";
                myUCConnexion.Size = new System.Drawing.Size(357, 126);
                myUCConnexion.TabIndex = 1;
                myUCConnexion.RightToLeft = RightToLeft.No;
                Controls.Add(myUCConnexion);
            }
        }

        public siteControlPanel() :
            base ()
        {
        }

        public siteControlPanel(SetText myOuputMethod,  SypPrefForm.SypPrefStatusText myStatusBar, SypSite mySite)
            : base(myOuputMethod)
        {
            StatusBarText = myStatusBar;
            this.mySite = mySite;
            InitPanel();
            RefreshConfig();
            SiteId = mySite.SiteId;
        }

        private void SiteControlPanelCheckBoxArchivage_CheckedChanged(object sender, EventArgs e)
        {
            StatusBarText(GetRessources("OptionsStatusSettingsApplied"));
        }

        private void SiteControlPanelCheckboxSync_CheckedChanged(object sender, EventArgs e)
        {
            StatusBarText(GetRessources("OptionsStatusSettingsChangeApplied"));
        }

        private void SiteControlPanelTextBoxUrl_Enter(object sender, EventArgs e)
        {
            StatusBarText(GetRessources("OptionsStatusHostnameTip"));
        }
    }
}
